<template>
  <div class="home">
    <el-container style="height: 100vh">
      <el-header height="6vh" class="home_header">
        <div><img src="@/static/img/地球.png" alt="" /></div>
        <div>
          <el-button type="text" @click="loginVisible = true" style="font-size:30px;font-weight: bold;">{{login==false?"未登录":login}}</el-button>
          <el-dialog
            :visible.sync="loginVisible"
            width="50%"
          >
            <div style="text-align:left;width:85%;margin: 20px auto;font-size:23px">登录</div>
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" style="text-align:center">
              <el-form-item label="账号" prop="uname" >
                <el-input type="text" v-model="ruleForm.uname" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="pass">
                <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
              </el-form-item>
              <span>
              <el-button @click="loginVisible = false">取 消</el-button>
              <el-button type="primary" @click="submitForm('ruleForm')"
                >登 录</el-button
              >
            </span>
            </el-form>

          </el-dialog>
        </div>
      </el-header>
      <el-container>
        <el-aside height="100vh" width="13vw" class="home_aside">
          <asidetab></asidetab>
        </el-aside>
        <el-main class="home_main">
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import asidetab from "@/components/AsideTable.vue";

export default {
  name: "HomeView",
  data() {
    var checkName = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入账号'));
        }
        callback();
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        }
        callback();
      };
    return {
      login: false,
      loginVisible: false,
      ruleForm: {
        uname: '',
        pass: '',
      },
      rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          uname: [
            { validator: checkName, trigger: 'blur' }
          ],
        }
    };
  },
  components: {
    asidetab,
  },
  mounted() {
    const login=this.$getItem("login")
    if(login==null||login==undefined){
      console.log(login)
      this.login=false
    }else {
      console.log(login)
      this.login=login.name
    }
  },
  methods: {
    //登录方法
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          //向后端请求查询用户是否存在,并返回用户的权限等级
          this.$axios.post(`api/login`,JSON.stringify({userName:this.ruleForm.uname,userPass:this.ruleForm.pass})).then((data)=>{
            this.login=data.data.name
            this.$setItem("login",{name:this.login,power:data.data.power})
          }).catch(function (error) {
            console.log(error);
          });
          this.loginVisible = false
          console.log(this.ruleForm.uname)
          this.$message({
            type: 'success',
            message: '登录成功!'
          });
        } else {
          console.log('error submit!!');
          return false
        }
      });
    },
  },
};
</script>

<style lang="scss">
.home_header {
  background: linear-gradient(90deg, rgb(67, 179, 173) 20%, rgb(158, 186, 229) 50%,rgb(0, 0, 0) 90%);
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: space-between;
  > div:nth-child(2) {
    color: white;
    text-align: center;
    width: 20%;
  }
  img {
    width: 442px;
    height: 302px;
  }
}
.home_aside {
  background: #545c64;
}
.home_main {
  // background: linear-gradient(1800deg, rgb(217, 217, 217) 20%, rgb(104, 104, 104) 90%);
  background: #eaecf1;
  padding: 0 20px!important;
}
</style>
